﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Member_ManagePosition : System.Web.UI.Page
{

    private String userName;

    protected void Page_Load(object sender, EventArgs e)
    {
        userName = Request["userID"];
        if (!Page.IsPostBack)
        {


            BindSingleUserInfo();
            BindAssignedUserInfo();
            BindUnAssignedUserInfo();
        }
    }

    private void BindUnAssignedUserInfo()
    {
        SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter();
        SQLExpressDAL.Users.AssignUserDataTable userTable = userAdp.GetUnAssignedUserInfo(userName);

        this.GVUnSettedPosition.DataSource = userTable;
        this.GVUnSettedPosition.DataBind();
    }

    private void BindAssignedUserInfo()
    {
        SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter();
        SQLExpressDAL.Users.AssignUserDataTable userTable = userAdp.GetAssignedUserInfo(userName);

        this.GVSettedPosition.DataSource = userTable;
        this.GVSettedPosition.DataBind();
    }

    private void BindSingleUserInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetSingleUserInfo(userName);
        SQLExpressDAL.Users.UsersRow userRow = null;

        if (userTable.Rows.Count > 0)
        {
            userRow = (SQLExpressDAL.Users.UsersRow)userTable.Rows[0];

            lblMemberAccount.Text = userRow.UserID;
            lblMemberName.Text = userRow.UserName;

        }
        else
        {
            return;
        }


    }

    protected void lbtnDel_Command(object sender, CommandEventArgs e)
    {
       

        string msg = "删除失败!";
        try
        {
            SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter();
            int flag = userAdp.CancelAssignedUser(e.CommandArgument.ToString());

            if (flag > 0)
            {
                msg = "删除成功!";
            }
        }
        catch (Exception ex)
        {
            msg = ex.Message.ToString();
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
            BindAssignedUserInfo();

            BindUnAssignedUserInfo();

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string msg = "安置失败!";

        try
        {
            String userID = Request["chksingle"];

            if (String.IsNullOrEmpty(userID))
            {
                msg = "未选择要安置的会员";
                return;
            }

            String[] userIDs = userID.Split(',');

            if (userIDs.Length > 2)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('每个会员最多安置两个成员!');", true);
                return;
            }

            SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter();
            SQLExpressDAL.Users.AssignUserDataTable userTable = userAdp.GetAssignedUserInfo(userName);
            int assignedCount = userTable.Rows.Count;
            String parentID = this.lblMemberAccount.Text.Trim();

            if (assignedCount == 2)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + string.Format("会员:{0}已经安置了两个成员!", parentID) + "');", true);
                return;
            }
            else if (assignedCount == 1 && userIDs.Length >= 2)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + string.Format("会员:{0}只能安置一个成员!", parentID) + "');", true);
                return;
            }
            else
            {
                msg = AddNewAssignedUser(parentID, userID);
            }

        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
            BindAssignedUserInfo();
            BindUnAssignedUserInfo();
        }
    }

    private string AddNewAssignedUser(string parentID, string userID)
    {
        string msg = "安置失败!";
        try
        {
            SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.AssignUserTableAdapter();
            int flag = userAdp.SetAssignedUser(parentID, userID);
            
            if (flag > 0)
            {
                msg = "安置成功!";
            }
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }

        return msg;

    }
}
